package com.purang.web.entity.vo;

import com.purang.web.common.BaseResponse;
import com.purang.web.common.CommonResponseCode;
import lombok.Getter;

/**
 * 统一返回VO
 *
 * @author Chuan Qin
 * @version 1.0-RELEASE
 * @since 1.0-RELEASE
 */
@Getter
public class ResponseVo<T> implements BaseResponse {

    private static final long serialVersionUID = 1L;

    private Integer code;

    private String message;

    private Boolean success;

    private T data;

    private ResponseVo(Integer code, String message, Boolean success, T data) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.data = data;
    }

    /**
     * 成功
     */
    public static ResponseVo ok() {
        return new ResponseVo(CommonResponseCode.OK.getCode(), CommonResponseCode.OK.getMessage(), true, null);
    }

    /**
     * 成功
     *
     * @param data 成功返回结果
     */
    public static <T> ResponseVo<T> ok(T data) {
        return new ResponseVo(CommonResponseCode.OK.getCode(), CommonResponseCode.OK.getMessage(), true, data);
    }

    /**
     * 失败
     */
    public static ResponseVo fail() {
        return new ResponseVo(CommonResponseCode.SERVER_ERROR.getCode(), CommonResponseCode.SERVER_ERROR.getMessage(), false, null);
    }

    /**
     * 失败
     *
     * @param code    失败状态码
     * @param message 失败信息
     */
    public static ResponseVo fail(Integer code, String message) {
        return new ResponseVo(code, message, false, null);
    }

    /**
     * 失败
     *
     * @param baseResponse {@link BaseResponse}
     */
    public static ResponseVo fail(BaseResponse baseResponse) {
        return new ResponseVo(baseResponse.getCode(), baseResponse.getMessage(), false, null);
    }
}
